package cn.kengtion.shopcart.Model;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.kengtion.shopcart.Bean.GoodBean;
import cn.kengtion.shopcart.Listener.AbsMsgListener;
import cn.kengtion.shopcart.Utils.OkHttpUtils;

/**
 * 创建时间 2017/10/30
 * 创建人 洪坤峰
 * 功能描述：
 */

public class CartModelImpl implements CartModel {
    private List<GoodBean> goodList = new ArrayList<>();
    private CartDataListener listener;
    private AbsMsgListener msgListener;

    public CartModelImpl(CartDataListener listener) {
        this.listener = listener;
    }


    @Override
    public void addGoods(String goodsId) {
        OkHttpUtils.ResultCallback<String> resultCallback = new OkHttpUtils.ResultCallback<String>() {
            @Override
            public void onSuccess(String response) {
                try {
                    JSONObject res = new JSONObject(response);
                    if (res.getInt("status") == -2) {
                        CartModelImpl.this.errorOccured("商品不存在");
                    } else {
                        JSONObject goodsInfo = res.getJSONObject("goodInfo");
                        GoodBean goodBean = new GoodBean();
                        goodBean.setGoodsId(goodsInfo.getString("id"));
                        goodBean.setNum(1);
                        goodBean.setGoodsName(goodsInfo.getString("goodName"));
                        goodBean.setRealPirce((double)(Math.round(goodsInfo.getDouble("promotePrice")*100)/100.0));
                        goodBean.setGoodsId(goodsInfo.getString("id"));
                        goodBean.setMarketPrice(goodsInfo.getDouble("goodPrice"));
                        goodBean.setGoodsImage(goodsInfo.getString("goodImage"));
                        goodList.add(goodBean);
                        listener.GoodsDataChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Exception e) {
                CartModelImpl.this.errorOccured("请求失败");
            }
        };
    }

    @Override
    public void deleteGoods(GoodBean goodBean) {
        goodList.remove(goodBean);
        listener.GoodsDataChanged();
    }

    @Override
    public void changeGoodsNum(GoodBean goodBean) {
        for (GoodBean good : goodList) {
            if (good.getGoodsId().equals(goodBean.getGoodsId()))
                good.setNum(goodBean.getNum());
        }
        listener.GoodsDataChanged();
    }

    @Override
    public void getGoodsList() {
        listener.getGoodsList(goodList);
    }

    @Override
    public void setMsgListener(AbsMsgListener listener) {
        this.msgListener = listener;
    }

    @Override
    public void errorOccured(String error) {

    }

    public interface CartDataListener {
        void GoodsDataChanged();

        void getGoodsList(List<GoodBean> goodBeans);
    }
}
